package com.neborosoft.android.events;

import android.os.Handler;
import com.neborosoft.lib.events.update.UpdateListener;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 06.01.13
 * Time: 15:26
 * To change this template use File | Settings | File Templates.
 */
public class UiUpdateListener implements UpdateListener{
    private UpdateListener updateListener;
    private Handler handler = new Handler();
    private Thread creationThread = Thread.currentThread();

    public UiUpdateListener(UpdateListener updateListener) {
        this.updateListener = updateListener;
        if(updateListener == null){
            throw new NullPointerException();
        }
    }

    @Override
    public void onUpdate() {
        if(creationThread == Thread.currentThread()){
            updateListener.onUpdate();
        }
        else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    updateListener.onUpdate();
                }
            });
        }
    }
}
